package com.tgu.home_action.service.impl.admin;

import com.tgu.home_action.dao.AdminMapper;
import com.tgu.home_action.dao.CaseMapper;
import com.tgu.home_action.dao.DynamicMapper;
import com.tgu.home_action.pojo.*;
import com.tgu.home_action.pojo.exception.IMException;
import com.tgu.home_action.pojo.state.RabbitMq;
import com.tgu.home_action.pojo.state.RedisHeader;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.admin.AdminCaseService;
import com.tgu.home_action.service.utils.IMService;
import org.slf4j.Logger;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class AdminCaseServiceImpl implements AdminCaseService {
    @Autowired
    private CaseMapper caseMapper;
    @Autowired
    private DynamicMapper dynamicMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private Logger logger;
    @Autowired
    private IMService imService;
    @Autowired
    private AdminMapper adminMapper;

    /**
     * 查询所有案件信息
     */
    @Override
    public ResponseData<Page<Case>> getCases(Case nowCase) {
        int sum = caseMapper.getAccordCaseNumber(nowCase);
        if (nowCase.getPageNo() == null || nowCase.getPageSize() == null) {
            nowCase.setPageNo(1);
            nowCase.setPageSize(sum);
        }
        nowCase.setStartSize((nowCase.getPageNo() - 1) * nowCase.getPageSize());
        List<Case> cases = caseMapper.getAccordCase(nowCase);
        int pageNum;
        if (nowCase.getPageSize() == 0) {
            pageNum = 0;
        } else {
            pageNum = sum / nowCase.getPageSize();
            if (sum % nowCase.getPageSize() > 0) {
                pageNum++;
            }
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), new Page<>(nowCase.getPageNo(), nowCase.getPageSize(), pageNum, sum, cases));

    }

    /**
     * 查询案件动态
     */
    @Override
    public ResponseData<List<Dynamic>> getCaseDynamic(Integer caseId) {
        List<Dynamic> dynamics = dynamicMapper.getDynamicByCaseId(caseId);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), dynamics);
    }

    /**
     * 结束案件
     */
    @Override
    public ResponseData<Object> endCase(Integer caseId, short state) {
        if (state != 2 && state != 4) {
            return new ResponseData<>("state只能是2或4", ResponseState.PARAM_IS_ERROR.getValue());
        }
        Case nowCase = caseMapper.getById(caseId);
        if (nowCase == null) {
            return new ResponseData<>(ResponseState.CASE_NOT_EXIST.getMessage(), ResponseState.CASE_NOT_EXIST.getValue());
        }
        if (nowCase.getState() != 1 && nowCase.getState() != 3) {
            return new ResponseData<>(ResponseState.OPERATION_HAI_FINISH.getMessage(), ResponseState.OPERATION_HAI_FINISH.getValue());
        }
        nowCase.setState(state);
        Boolean isSuccess = caseMapper.changeCaseState(caseId, state);
        //设置完成时间
        nowCase.setEndTime(new Timestamp(new Date().getTime()));
        caseMapper.setEndTime(nowCase);
        if (isSuccess) {
            System.out.println("完成state修改");
            rabbitTemplate.convertAndSend(RabbitMq.CASE_EXCHANGE.getValue(), RabbitMq.CASE_FINISH.getValue(), nowCase);
//            rabbitTemplate.convertAndSend(RabbitMq.CASE_EXCHANGE.getValue(),RabbitMq.CASE_END.getValue(),nowCase);
            return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
        } else {
            return new ResponseData<>(ResponseState.CASE_NOT_EXIST.getMessage(), ResponseState.CASE_NOT_EXIST.getValue());
        }
    }

    /**
     * 管理员加入/退出案件群组
     *  @param caseId ：案件Id
     * @param type :       1为加入，2为退出
     */
    @Override
    public ResponseData<String> joinGroup(Integer caseId, int type, Admin admin) throws IMException {
        String groupId = stringRedisTemplate.opsForValue().get(RedisHeader.GROUP_ID.getHeader() + caseId);
        if (groupId == null) {
            logger.error("该案件不存在群组");
            return new ResponseData<>("该案件不存在群组", ResponseState.ERROR.getValue());
        }
        Boolean isExist = isExist = imService.accountCheck("admin_" + admin.getId());
        if (!isExist) {
            imService.insertUser("admin_" + admin.getId(), adminMapper.getNameById(admin.getId()), null);
            imService.setMessage("admin_" + admin.getId(), "admin", admin.getId());
        }
        if (type == 1) {
            imService.addMember(groupId, "admin_" + admin.getId());
            return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), groupId);
        } else if (type == 2) {
            imService.deleteMember(groupId, "admin_" + admin.getId());
            return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(),groupId);
        } else {
            return new ResponseData<>("type只能是1或2", ResponseState.PARAM_IS_ERROR.getValue());
        }
    }
}
